<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-02-22
 * Time: 16:49:00
 * Info: 后台配置控制器
 */

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\Config as ConfigModel;
use think\facade\Db;
use think\facade\View;
use libs\Form;

class Config extends Base
{

    /**
     * 后台配置列表
     */
    public function index()
    {
        $groupName = ConfigModel::GROUP_LIST;

        // 获取配置表中的分组group字段
        $groupList = ConfigModel::order('weigh asc')->column('group');
        if ( ! empty($groupList)) {
            $groupList = array_unique($groupList);
        }

        $group2 = [];
        foreach ($groupName as $k => $v) {
            if (in_array($k, array_values($groupList))) {
                array_push($group2, ['group_en' => $k, 'group_name' => $v]);
            }
        }

        $newList = [];
        $list    = ConfigModel::where('group','<>','seo')->order(['weigh' => 'asc', 'id' => 'asc'])->select()->toArray();
        foreach ($list as $k => $v) {
            if ($v['fieldtype'] == 'radio' || $v['fieldtype'] == 'select') {
                $vSetting   = ! empty($v['setting']) ? json_decode($v['setting'], true) : '';
                $newSetting = [];
                foreach ($vSetting as $s) {
                    if (strpos($s, ':') !== false) {
                        $sArr                 = explode(":", $s);
                        $newSetting[$sArr[1]] = $sArr[0];
                        $v['setting']         = ($newSetting);
                    } else {
                        $v['setting'] = $vSetting;
                    }
                }
            }
            $newList[$v['group']][] = $v;
        }
        View::assign('lists', $newList);
        View::assign('group', $group2);

        return View::fetch();
    }

    /**
     * 保存设置
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param('row');
            $res    = ConfigModel::saveData($params);

            $this->success($res['msg'], '', '', $res['url']);
        }
    }

    /**
     * 自定义配置
     */
    public function customConfig()
    {
        $data = ConfigModel::where('group', 99)->select();
        View::assign('data', $data);

        return View::fetch();
    }

    /**
     * 添加自定义配置
     */
    public function customConfigAdd()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $res = ConfigModel::customAdd($param);

            return json($res);

        }

        return View::fetch();
    }

    /**
     * 修改自定义配置
     */
    public function customConfigEdit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $res = ConfigModel::customEdit($param);

            return json($res);

        }

        $id   = $this->request->param('id');
        $data = ConfigModel::where(['id' => $id])->find();
        View::assign('data', $data);

        return View::fetch();
    }

    /**
     * 删除自定义配置
     */
    public function customConfigDel()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id');
            ConfigModel::customDel($id);
            $this->success('删除成功');
        }
    }

}
